home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / aplictns / dkbtrace / part03 < prev    next >
Encoding:
Internet Message Format  |  1990-09-03  |  66.7 KB

  1. Path: abcfd20.larc.nasa.gov!amiga-request
  2. From: amiga-request@abcfd20.larc.nasa.gov (Amiga Sources/Binaries Moderator)
  3. Subject: v90i251: DKBTrace 2.01 - DKBtrace Ray-Tracer, Part03/10
  4. Reply-To: David Schanen <mtv@milton.u.washington.edu>
  5. Newsgroups: comp.sources.amiga
  6. Message-ID: <comp.sources.amiga:v90i251@abcfd20.larc.nasa.gov>
  7. References: <comp.sources.amiga:v90i249@abcfd20.larc.nasa.gov>
  8. Date: 03 Sep 90 23:21:48 GMT
  9. Approved: tadguy@uunet.UU.NET (Tad Guy)
  10. X-Mail-Submissions-To: amiga@uunet.uu.net
  11. X-Post-Discussions-To: comp.sys.amiga
  12.  
  13. Submitted-by: David Schanen <mtv@milton.u.washington.edu>
  14. Posting-number: Volume 90, Issue 251
  15. Archive-name: applications/dkbtrace-2.01/part03
  16.  
  17. #!/bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of archive 3 (of 10)."
  24. # Contents:  src/Sculpt2dkb.c src/amiga.c src/csg.c src/gif.c
  25. #   src/iffw.c src/matrices.c src/planes.c src/spheres.c
  26. # Wrapped by tadguy@abcfd20 on Mon Sep  3 19:21:17 1990
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f 'src/Sculpt2dkb.c' -a "${1}" != "-c" ; then 
  29.   echo shar: Will not clobber existing file \"'src/Sculpt2dkb.c'\"
  30. else
  31. echo shar: Extracting \"'src/Sculpt2dkb.c'\" \(7547 characters\)
  32. sed "s/^X//" >'src/Sculpt2dkb.c' <<'END_OF_FILE'
  33. X/*****************************************************************************
  34. X*
  35. X*                                    Sculpt2dat.c
  36. X*
  37. X*   from DKBTrace (c) 1990  David Buck
  38. X*
  39. X*  This program reads a Sculpt-Animate 3D format scene file and generates an
  40. X*  output file that may be read in to DKBTrace.
  41. X*
  42. X* This software is freely distributable. The source and/or object code may be
  43. X* copied or uploaded to communications services so long as this notice remains
  44. X* at the top of each file.  If any changes are made to the program, you must
  45. X* clearly indicate in the documentation and in the programs startup message
  46. X* who it was who made the changes. The documentation should also describe what
  47. X* those changes were. This software may not be included in whole or in
  48. X* part into any commercial package without the express written consent of the
  49. X* author.  It may, however, be included in other public domain or freely
  50. X* distributed software so long as the proper credit for the software is given.
  51. X*
  52. X* This software is provided as is without any guarantees or warranty. Although
  53. X* the author has attempted to find and correct any bugs in the software, he
  54. X* is not responsible for any damage caused by the use of the software.  The
  55. X* author is under no obligation to provide service, corrections, or upgrades
  56. X* to this package.
  57. X*
  58. X* Despite all the legal stuff above, if you do find bugs, I would like to hear
  59. X* about them.  Also, if you have any comments or questions, you may contact me
  60. X* at the following address:
  61. X*
  62. X*     David Buck
  63. X*     22C Sonnet Cres.
  64. X*     Nepean Ontario
  65. X*     Canada, K2H 8W7
  66. X*
  67. X*  I can also be reached on the following bulleton boards:
  68. X*
  69. X*     ATX              (613) 526-4141
  70. X*     OMX              (613) 731-3419
  71. X*     Mystic           (613) 731-0088 or (613) 731-6698
  72. X*
  73. X*  Fidonet:   1:163/109.9
  74. X*  Internet:  David_Buck@Carleton.CA
  75. X*
  76. X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  77. X*
  78. X*     Lattice BBS                      (708) 916-1200
  79. X*     The Information Exchange BBS     (708) 945-5575
  80. X*     Stillwaters BBS                  (708) 403-2826
  81. X*
  82. X*****************************************************************************/
  83. X
  84. X
  85. X#include <stdio.h>
  86. X#include "frame.h"
  87. X#include "dkbproto.h"
  88. X
  89. Xvoid read_iff_file(void);
  90. X
  91. XIMAGE_COLOUR *iff_colour_map;
  92. Xint colourmap_size;
  93. XFILE *f, *out;
  94. X
  95. Xstatic CHUNK_HEADER Chunk_Header;
  96. X
  97. XDBL *vertices_x, *vertices_y, *vertices_z;
  98. XDBL max_x, max_y, max_z, min_x, min_y, min_z;
  99. X#define FABS(x) ((x<0.0)?-x:x)
  100. X
  101. X#define FORM 0x464f524dL
  102. X#define SC3D 0x53433344L
  103. X#define VERT 0x56455254L
  104. X#define FACE 0x46414345L
  105. X
  106. X#define CMPNONE 0
  107. X
  108. Xchar *Textures[7] = {"Dull", "Shiny", "Mirror", "Luminous", "Glass", "Metal", "Glass2"};
  109. X
  110. Xvoid iff_error ()
  111. X   {
  112. X   printf ("Invalid iff file\n");
  113. X   exit(1);
  114. X   }
  115. X
  116. Xint read_byte(f)
  117. X   FILE *f;
  118. X   {
  119. X   int c;
  120. X   if ((c = getc(f)) == EOF)
  121. X      iff_error();
  122. X   return (c);
  123. X   }
  124. X
  125. Xint read_word(f)
  126. X   FILE *f;
  127. X   {
  128. X   int result;
  129. X
  130. X   result = read_byte(f)*256;
  131. X   result += read_byte(f);
  132. X   return (result);
  133. X   }
  134. X
  135. Xlong read_long(f)
  136. X   FILE *f;
  137. X   {
  138. X   int i;
  139. X   long result;
  140. X
  141. X   result = 0;
  142. X   for (i = 0 ; i < 4 ; i++)
  143. X      result = result * 256 + read_byte(f);
  144. X
  145. X   return (result);
  146. X   }
  147. X
  148. Xvoid Read_Chunk_Header (f, dest)
  149. X   FILE *f;
  150. X   CHUNK_HEADER *dest;
  151. X   {
  152. X   dest->name = read_long(f);
  153. X   dest->size = (int) read_long(f);
  154. X   }
  155. X
  156. Xvoid read_iff_file()
  157. X   {
  158. X   int i;
  159. X   int vert1, vert2, vert3;
  160. X   unsigned int texture;
  161. X   DBL red, green, blue;
  162. X
  163. X   max_x = max_y = max_z = -10000000.0;
  164. X   min_x = min_y = min_z = 10000000.0;
  165. X
  166. X   while (1) {
  167. X      Read_Chunk_Header(f, &Chunk_Header);
  168. X      switch ((int) Chunk_Header.name) {
  169. X         case FORM: if (read_long(f) != SC3D)
  170. X                       iff_error();
  171. X                    break;
  172. X
  173. X         case VERT: vertices_x = (DBL *)
  174. X                        malloc (Chunk_Header.size * (sizeof(DBL)/sizeof(long)));
  175. X                    vertices_y = (DBL *)
  176. X                        malloc (Chunk_Header.size * (sizeof(DBL)/sizeof(long)));
  177. X                    vertices_z = (DBL *)
  178. X                        malloc (Chunk_Header.size * (sizeof(DBL)/sizeof(long)));
  179. X                    for (i = 0 ; i < Chunk_Header.size/12 ; i++) {
  180. X                       vertices_x[i] = read_long(f) / 10000.0;
  181. X                       if (vertices_x[i] < min_x)
  182. X                          min_x = vertices_x[i];
  183. X                       if (vertices_x[i] > max_x)
  184. X                          max_x = vertices_x[i];
  185. X
  186. X                       vertices_y[i] = read_long(f) / 10000.0;
  187. X                       if (vertices_y[i] < min_y)
  188. X                          min_y = vertices_y[i];
  189. X                       if (vertices_y[i] > max_y)
  190. X                          max_y = vertices_y[i];
  191. X
  192. X                       vertices_z[i] = read_long(f) / 10000.0;
  193. X                       if (vertices_z[i] < min_z)
  194. X                          min_z = vertices_z[i];
  195. X                       if (vertices_z[i] > max_z)
  196. X                          max_z = vertices_z[i];
  197. X                       }
  198. X                    break;
  199. X
  200. X         case FACE: for (i = 0 ; i < Chunk_Header.size/16 ; i++) {
  201. X                       vert1 = (int) read_long(f);
  202. X                       vert2 = (int) read_long(f);
  203. X                       vert3 = (int) read_long(f);
  204. X                       red = read_byte(f) / 255.0;
  205. X                       green = read_byte(f) / 255.0;
  206. X                       blue = read_byte(f) / 255.0;
  207. X
  208. X               texture = read_byte(f);
  209. X               texture &= 0x07;        /* mask upper bits */
  210. X
  211. X               fprintf (out, "OBJECT TRIANGLE <%f %f %f> <%f %f %f> <%f %f %f> END_TRIANGLE COLOUR RED %f GREEN %f BLUE %f TEXTURE %s END_TEXTURE END_OBJECT\n",
  212. X                          vertices_x[vert1], vertices_y[vert1], vertices_z[vert1],
  213. X                          vertices_x[vert2], vertices_y[vert2], vertices_z[vert2],
  214. X                          vertices_x[vert3], vertices_y[vert3], vertices_z[vert3],
  215. X                          red, green, blue, Textures[texture]);
  216. X                       }
  217. X                    return;
  218. X
  219. X         default:
  220. X            for (i = 0 ; i < Chunk_Header.size ; i++)
  221. X               if (getc(f) == EOF)
  222. X                  iff_error();
  223. X            break;
  224. X         }
  225. X      }
  226. X   }
  227. X
  228. Xvoid main (argc, argv)
  229. X   int argc;
  230. X   char **argv;
  231. X   {
  232. X   if (argc != 3) {
  233. X      printf ("Usage: %s <scene-file> <output-file>\n", argv[0]);
  234. X      exit (1);
  235. X      }
  236. X   
  237. X   if ((f = fopen(argv[1], "rb")) == NULL) {
  238. X      printf ("Cannot open IFF file %s\n", argv[1]);
  239. X      exit(1);
  240. X      }
  241. X
  242. X   if ((out = fopen(argv[2], "w")) == NULL) {
  243. X      printf ("Cannot open output file %s\n", argv[1]);
  244. X      exit(1);
  245. X      }
  246. X   
  247. X   fprintf(out, "COMPOSITE\n");
  248. X   read_iff_file();
  249. X   fprintf(out, "   BOUNDED_BY\n      INTERSECTION\n");
  250. X   fprintf(out, "         PLANE <1.0  0.0  0.0> %1.02f END_PLANE\n", FABS(max_x) * 1.01);
  251. X   fprintf(out, "         PLANE <-1.0 0.0  0.0> %1.02f END_PLANE\n", FABS(min_x) * 1.01);
  252. X   fprintf(out, "         PLANE <0.0  1.0  0.0> %1.02f END_PLANE\n", FABS(max_y) * 1.01);
  253. X   fprintf(out, "         PLANE <0.0 -1.0  0.0> %1.02f END_PLANE\n", FABS(min_y) * 1.01);
  254. X   fprintf(out, "         PLANE <0.0  0.0  1.0> %1.02f END_PLANE\n", FABS(max_z) * 1.01);
  255. X   fprintf(out, "         PLANE <0.0  0.0 -1.0> %1.02f END_PLANE\n", FABS(min_z) * 1.01);
  256. X   fprintf(out, "      END_INTERSECTION\n   END_BOUND\n   \nEND_COMPOSITE\n");
  257. X   printf ("X values range from %f to %f\n", min_x, max_x);
  258. X   printf ("Y values range from %f to %f\n", min_y, max_y);
  259. X   printf ("Z values range from %f to %f\n", min_z, max_z);
  260. X   fclose(f);
  261. X   fclose(out);
  262. X   }
  263. END_OF_FILE
  264. if test 7547 -ne `wc -c <'src/Sculpt2dkb.c'`; then
  265.     echo shar: \"'src/Sculpt2dkb.c'\" unpacked with wrong size!
  266. fi
  267. # end of 'src/Sculpt2dkb.c'
  268. fi
  269. if test -f 'src/amiga.c' -a "${1}" != "-c" ; then 
  270.   echo shar: Will not clobber existing file \"'src/amiga.c'\"
  271. else
  272. echo shar: Extracting \"'src/amiga.c'\" \(6939 characters\)
  273. sed "s/^X//" >'src/amiga.c' <<'END_OF_FILE'
  274. X/*****************************************************************************
  275. X*
  276. X*                                   amiga.c
  277. X*
  278. X*   from DKBTrace (c) 1990  David Buck
  279. X*
  280. X*  This module handles all of the Amiga-specific code for the raytracer.
  281. X*
  282. X*
  283. X* This software is freely distributable. The source and/or object code may be
  284. X* copied or uploaded to communications services so long as this notice remains
  285. X* at the top of each file.  If any changes are made to the program, you must
  286. X* clearly indicate in the documentation and in the programs startup message
  287. X* who it was who made the changes. The documentation should also describe what
  288. X* those changes were. This software may not be included in whole or in
  289. X* part into any commercial package without the express written consent of the
  290. X* author.  It may, however, be included in other public domain or freely
  291. X* distributed software so long as the proper credit for the software is given.
  292. X*
  293. X* This software is provided as is without any guarantees or warranty. Although
  294. X* the author has attempted to find and correct any bugs in the software, he
  295. X* is not responsible for any damage caused by the use of the software.  The
  296. X* author is under no obligation to provide service, corrections, or upgrades
  297. X* to this package.
  298. X*
  299. X* Despite all the legal stuff above, if you do find bugs, I would like to hear
  300. X* about them.  Also, if you have any comments or questions, you may contact me
  301. X* at the following address:
  302. X*
  303. X*     David Buck
  304. X*     22C Sonnet Cres.
  305. X*     Nepean Ontario
  306. X*     Canada, K2H 8W7
  307. X*
  308. X*  I can also be reached on the following bulleton boards:
  309. X*
  310. X*     ATX              (613) 526-4141
  311. X*     OMX              (613) 731-3419
  312. X*     Mystic           (613) 731-0088 or (613) 731-6698
  313. X*
  314. X*  Fidonet:   1:163/109.9
  315. X*  Internet:  David_Buck@Carleton.CA
  316. X*
  317. X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  318. X*
  319. X*     Lattice BBS                      (708) 916-1200
  320. X*     The Information Exchange BBS     (708) 945-5575
  321. X*     Stillwaters BBS                  (708) 403-2826
  322. X*
  323. X*****************************************************************************/
  324. X
  325. X#include "frame.h"
  326. X#include "dkbproto.h"
  327. X
  328. X#include <proto/exec.h>
  329. X#include <proto/intuition.h>
  330. X#include <proto/graphics.h>
  331. X#include <proto/dos.h>
  332. X#include <exec/types.h>
  333. X#include <intuition/intuition.h>
  334. X#include <graphics/display.h>
  335. X
  336. Xvoid geta4(void);
  337. Xvoid Requestor_Handler(void);
  338. Xvoid Amiga_open(void);
  339. Xvoid Amiga_close(void);
  340. Xvoid open_requestor(void);
  341. Xvoid close_requestor(void);
  342. Xextern int Options;
  343. X
  344. X#define INT_REV 29L
  345. X#define GR_REV 29L
  346. X
  347. Xstruct IntuitionBase *IntuitionBase;
  348. Xstruct GfxBase *GfxBase;
  349. Xstruct Screen *s;
  350. Xstruct Window *w;
  351. Xstruct Task *Requestor_Task;
  352. X
  353. Xvolatile int Requestor_Running;
  354. Xvolatile extern int Stop_Flag;
  355. X
  356. X#define SCREEN_WIDTH 320
  357. X#define SCREEN_HEIGHT 400
  358. X
  359. Xstruct NewScreen MyScreen =
  360. X   {
  361. X   0, 0,
  362. X   SCREEN_WIDTH, SCREEN_HEIGHT,
  363. X   6,
  364. X   0, 1,
  365. X   INTERLACE | HAM,
  366. X   SCREENQUIET,
  367. X   NULL,
  368. X   (UBYTE *) "DKB Ray Trace",
  369. X   NULL,
  370. X   NULL
  371. X   };
  372. X
  373. Xstruct Window *Requestor_Window;
  374. Xstruct MsgPort *Requestor_Port;
  375. X
  376. Xstruct IntuiText Body_Text =
  377. X   {0, 1, JAM1, 5, 10, NULL, (UBYTE *) "Click to abort the picture", NULL};
  378. X
  379. Xstruct IntuiText Abort_Text =
  380. X   {0, 1, JAM1, 5, 3, NULL, (UBYTE *) "Abort", NULL};
  381. X
  382. XUWORD ColorTbl[16] = { 0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666,
  383. X                       0x777, 0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd,
  384. X                       0xeee, 0xfff };
  385. X
  386. XLONG last_red = 0, last_green = 0, last_blue = 0, last_y = -1;
  387. X
  388. Xvoid Requestor_Handler ()
  389. X   {
  390. X   Requestor_Port = CreatePort ("ray trace port", 0L);
  391. X   Requestor_Window = BuildSysRequest
  392. X             (NULL, &Body_Text, NULL, &Abort_Text, GADGETUP, 280L, 60L);
  393. X   Wait ((1 << Requestor_Port -> mp_SigBit)
  394. X          | (1 << Requestor_Window -> UserPort -> mp_SigBit));
  395. X
  396. X   Requestor_Running = FALSE;
  397. X   Stop_Flag = TRUE;
  398. X   }
  399. X
  400. Xvoid Amiga_open()
  401. X   {
  402. X   IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",INT_REV);
  403. X   if (IntuitionBase == NULL)
  404. X     exit(FALSE);
  405. X
  406. X   GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", GR_REV);
  407. X   if (GfxBase == NULL)
  408. X     exit(FALSE);
  409. X   Requestor_Running = FALSE;
  410. X   }
  411. X
  412. Xvoid Amiga_close()
  413. X   {
  414. X   if (Requestor_Running) {
  415. X      Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  416. X      Delay (2L);
  417. X      }
  418. X
  419. X   if (Requestor_Window)
  420. X      FreeSysRequest (Requestor_Window);
  421. X
  422. X   Requestor_Window = NULL;
  423. X
  424. X   CloseLibrary (GfxBase) ;
  425. X   CloseLibrary (IntuitionBase) ;
  426. X   }
  427. X
  428. Xvoid open_requestor()
  429. X   {
  430. X   Requestor_Window = NULL;
  431. X   Stop_Flag = FALSE;
  432. X   Requestor_Running = TRUE;
  433. X   Requestor_Task = CreateTask ("Raytrace Requestor", 2L,
  434. X                                (APTR) Requestor_Handler, 20000L);
  435. X   }
  436. X
  437. Xvoid display_finished ()
  438. X   {
  439. X   if (Requestor_Running) {
  440. X     Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  441. X     Delay (2L);
  442. X     }
  443. X
  444. X   if (Requestor_Window)
  445. X      FreeSysRequest (Requestor_Window);
  446. X
  447. X   Requestor_Window = NULL;
  448. X   if (Options & PROMPTEXIT)
  449. X      {
  450. X      printf ("Finished.\nPress CR to quit.\n");
  451. X      getchar();
  452. X      }
  453. X   }
  454. X
  455. Xvoid display_init ()
  456. X   {
  457. X   Amiga_open();
  458. X   open_requestor();
  459. X
  460. X   Delay (10);
  461. X   if ((s = (struct Screen *) OpenScreen (&MyScreen)) == NULL)
  462. X      exit (FALSE);
  463. X
  464. X   ShowTitle (s, FALSE);
  465. X
  466. X   LoadRGB4 (&(s->ViewPort), ColorTbl, 16L);
  467. X   SetAPen (&(s->RastPort), 7L);
  468. X   RectFill (&(s -> RastPort), 0L, 0L, 319L, 399L);
  469. X   }
  470. X
  471. Xvoid display_close ()
  472. X   {
  473. X   if (Requestor_Running) {
  474. X      Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  475. X      Delay (2L);
  476. X      }
  477. X
  478. X   if (Requestor_Window)
  479. X      FreeSysRequest (Requestor_Window);
  480. X
  481. X   Requestor_Window = NULL;
  482. X
  483. X   CloseScreen (s);
  484. X   }
  485. X
  486. X#define absdif(x,y) ((x > y) ? (x - y) : (y - x))
  487. X#define max3(x,y,z) ((x>y)?((x>z)?1:3):((y>z)?2:3))
  488. X
  489. Xvoid display_plot (x, y, Red, Green, Blue)
  490. X   int x, y;
  491. X   char Red, Green, Blue;
  492. X   {
  493. X   register LONG delta_red, delta_green, delta_blue, colour;
  494. X   static int Last_Colour = 0;
  495. X
  496. X   if ((x >= SCREEN_WIDTH-1 )  || (y >= SCREEN_HEIGHT))
  497. X      return;
  498. X
  499. X   Red = (Red >> 4) & 0x0F;
  500. X   Green = (Green >> 4) & 0x0F;
  501. X   Blue = (Blue >> 4) & 0x0F;
  502. X
  503. X   if (last_y != y) {
  504. X      last_y = y;
  505. X      last_red = last_green = last_blue = 0;
  506. X      Last_Colour = 0;
  507. X      SetAPen (&(s -> RastPort), 0);
  508. X      WritePixel (&(s -> RastPort), 0, y);
  509. X      }
  510. X
  511. X   delta_red = absdif (Red, last_red);
  512. X   delta_green = absdif (Green, last_green);
  513. X   delta_blue = absdif (Blue, last_blue);
  514. X
  515. X   switch (max3(delta_red, delta_green, delta_blue)) {
  516. X      case 1:
  517. X         last_red = Red;
  518. X         colour = 0x20 + Red;
  519. X         break;
  520. X      case 2:
  521. X         last_green = Green;
  522. X         colour = 0x30 + Green;
  523. X         break;
  524. X      case 3:
  525. X         last_blue = Blue;
  526. X         colour = 0x10 + Blue;
  527. X         break;
  528. X      }
  529. X
  530. X   if (colour != Last_Colour)
  531. X      {
  532. X      SetAPen (&(s -> RastPort), colour);
  533. X      Last_Colour = colour;
  534. X      }
  535. X   WritePixel (&(s -> RastPort), x+1, y);
  536. X   }
  537. X
  538. END_OF_FILE
  539. if test 6939 -ne `wc -c <'src/amiga.c'`; then
  540.     echo shar: \"'src/amiga.c'\" unpacked with wrong size!
  541. fi
  542. # end of 'src/amiga.c'
  543. fi
  544. if test -f 'src/csg.c' -a "${1}" != "-c" ; then 
  545.   echo shar: Will not clobber existing file \"'src/csg.c'\"
  546. else
  547. echo shar: Extracting \"'src/csg.c'\" \(7895 characters\)
  548. sed "s/^X//" >'src/csg.c' <<'END_OF_FILE'
  549. X/*****************************************************************************
  550. X*
  551. X*                                    csg.c
  552. X*
  553. X*   from DKBTrace (c) 1990  David Buck
  554. X*
  555. X*  This module implements routines for constructive solid geometry.
  556. X*
  557. X* This software is freely distributable. The source and/or object code may be
  558. X* copied or uploaded to communications services so long as this notice remains
  559. X* at the top of each file.  If any changes are made to the program, you must
  560. X* clearly indicate in the documentation and in the programs startup message
  561. X* who it was who made the changes. The documentation should also describe what
  562. X* those changes were. This software may not be included in whole or in
  563. X* part into any commercial package without the express written consent of the
  564. X* author.  It may, however, be included in other public domain or freely
  565. X* distributed software so long as the proper credit for the software is given.
  566. X*
  567. X* This software is provided as is without any guarantees or warranty. Although
  568. X* the author has attempted to find and correct any bugs in the software, he
  569. X* is not responsible for any damage caused by the use of the software.  The
  570. X* author is under no obligation to provide service, corrections, or upgrades
  571. X* to this package.
  572. X*
  573. X* Despite all the legal stuff above, if you do find bugs, I would like to hear
  574. X* about them.  Also, if you have any comments or questions, you may contact me
  575. X* at the following address:
  576. X*
  577. X*     David Buck
  578. X*     22C Sonnet Cres.
  579. X*     Nepean Ontario
  580. X*     Canada, K2H 8W7
  581. X*
  582. X*  I can also be reached on the following bulleton boards:
  583. X*
  584. X*     ATX              (613) 526-4141
  585. X*     OMX              (613) 731-3419
  586. X*     Mystic           (613) 731-0088 or (613) 731-6698
  587. X*
  588. X*  Fidonet:   1:163/109.9
  589. X*  Internet:  David_Buck@Carleton.CA
  590. X*
  591. X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  592. X*
  593. X*     Lattice BBS                      (708) 916-1200
  594. X*     The Information Exchange BBS     (708) 945-5575
  595. X*     Stillwaters BBS                  (708) 403-2826
  596. X*
  597. X*****************************************************************************/
  598. X
  599. X
  600. X#include "frame.h"
  601. X#include "vector.h"
  602. X#include "dkbproto.h"
  603. X
  604. XMETHODS CSG_Union_Methods =
  605. X   { Object_Intersect, All_CSG_Union_Intersections,
  606. X     Inside_CSG_Union, NULL,
  607. X     Copy_CSG,
  608. X     Translate_CSG, Rotate_CSG,
  609. X     Scale_CSG, Invert_CSG};
  610. X
  611. XMETHODS CSG_Intersection_Methods =
  612. X   { Object_Intersect, All_CSG_Intersection_Intersections,
  613. X     Inside_CSG_Intersection, NULL,
  614. X     Copy_CSG,
  615. X     Translate_CSG, Rotate_CSG,
  616. X     Scale_CSG, Invert_CSG};
  617. X
  618. Xextern RAY *VP_Ray;
  619. Xextern unsigned long Options;
  620. X
  621. Xint All_CSG_Union_Intersections (Object, Ray, Depth_Queue)
  622. X   OBJECT *Object;
  623. X   RAY *Ray;
  624. X   PRIOQ *Depth_Queue;
  625. X   {
  626. X   register int Intersection_Found;
  627. X   CSG_SHAPE *Shape = (CSG_SHAPE *) Object;
  628. X   SHAPE *Local_Shape;
  629. X
  630. X   Intersection_Found = FALSE;
  631. X   for (Local_Shape = Shape -> Shapes;
  632. X        Local_Shape != NULL ;
  633. X        Local_Shape = Local_Shape -> Next_Object)
  634. X
  635. X       if (All_Intersections ((OBJECT *) Local_Shape, Ray, Depth_Queue))
  636. X           Intersection_Found = TRUE;
  637. X
  638. X   return (Intersection_Found);
  639. X   }
  640. X
  641. Xint All_CSG_Intersection_Intersections (Object, Ray, Depth_Queue)
  642. X   OBJECT *Object;
  643. X   RAY *Ray;
  644. X   PRIOQ *Depth_Queue;
  645. X   {
  646. X   int Intersection_Found, Any_Intersection_Found;
  647. X   CSG_SHAPE *Shape = (CSG_SHAPE *) Object;
  648. X   SHAPE *Local_Shape, *Shape2;
  649. X   PRIOQ *Local_Depth_Queue;
  650. X   INTERSECTION *Local_Intersection;
  651. X
  652. X   Local_Depth_Queue = pq_new (128);
  653. X
  654. X   Any_Intersection_Found = FALSE;
  655. X
  656. X   for (Local_Shape = Shape -> Shapes ;
  657. X        Local_Shape != NULL ;
  658. X        Local_Shape = Local_Shape -> Next_Object) {
  659. X
  660. X      All_Intersections ((OBJECT *) Local_Shape, Ray, Local_Depth_Queue);
  661. X
  662. X      for (Local_Intersection = pq_get_highest (Local_Depth_Queue);
  663. X           Local_Intersection != NULL ;
  664. X           pq_delete_highest (Local_Depth_Queue),
  665. X           Local_Intersection = pq_get_highest (Local_Depth_Queue)) {
  666. X
  667. X         Intersection_Found = TRUE;
  668. X
  669. X         for (Shape2 = Shape -> Shapes;
  670. X              Shape2 != NULL ;
  671. X              Shape2 = Shape2 -> Next_Object)
  672. X
  673. X            if (Shape2 != Local_Shape)
  674. X               if (!Inside (&Local_Intersection -> Point, (OBJECT *) Shape2)) {
  675. X                 Intersection_Found = FALSE;
  676. X                 break;
  677. X                 }
  678. X
  679. X         if (Intersection_Found) {
  680. X            pq_add (Depth_Queue, Local_Intersection);
  681. X            Any_Intersection_Found = TRUE;
  682. X            }
  683. X         }
  684. X      }
  685. X
  686. X   pq_free (Local_Depth_Queue);
  687. X
  688. X   return (Any_Intersection_Found);
  689. X   }
  690. X
  691. Xint Inside_CSG_Union (Point, Object)
  692. X   VECTOR *Point;
  693. X   OBJECT *Object;
  694. X   {
  695. X   CSG_SHAPE *Shape = (CSG_SHAPE *) Object;
  696. X   SHAPE *Local_Shape;
  697. X
  698. X   for (Local_Shape = Shape -> Shapes ;
  699. X        Local_Shape != NULL ;
  700. X        Local_Shape = Local_Shape -> Next_Object)
  701. X
  702. X      if (Inside (Point, (OBJECT *) Local_Shape))
  703. X         return (TRUE);
  704. X   return (FALSE);
  705. X   }
  706. X
  707. Xint Inside_CSG_Intersection (Point, Object)
  708. X   OBJECT *Object;
  709. X   VECTOR *Point;
  710. X   {
  711. X   SHAPE *Local_Shape;
  712. X   CSG_SHAPE *Shape = (CSG_SHAPE *) Object;
  713. X
  714. X   for (Local_Shape = Shape -> Shapes ;
  715. X        Local_Shape != NULL ;
  716. X        Local_Shape = Local_Shape -> Next_Object)
  717. X
  718. X      if (!Inside (Point, (OBJECT *) Local_Shape))
  719. X          return (FALSE);
  720. X
  721. X   return (TRUE);
  722. X   }
  723. X
  724. Xvoid *Copy_CSG (Object)
  725. X   OBJECT *Object;
  726. X   {
  727. X   CSG_SHAPE *Shape = (CSG_SHAPE *) Object;
  728. X   CSG_SHAPE *New_Shape;
  729. X   SHAPE *Local_Shape, *Copied_Shape;
  730. X
  731. X   New_Shape = Get_CSG_Shape ();
  732. X   New_Shape->Methods = Shape->Methods;
  733. X   New_Shape->Type = Shape->Type;
  734. X   New_Shape -> Next_Object = NULL;
  735. X   New_Shape -> Shapes = NULL;
  736. X
  737. X   for (Local_Shape = Shape -> Shapes;
  738. X        Local_Shape != NULL ;
  739. X        Local_Shape = Local_Shape -> Next_Object) {
  740. X
  741. X      Copied_Shape = (SHAPE *) Copy ((OBJECT *) Local_Shape);
  742. X      Link ((OBJECT *) Copied_Shape,
  743. X            (OBJECT **) &(Copied_Shape -> Next_Object),
  744. X            (OBJECT **) &(New_Shape -> Shapes));
  745. X      }
  746. X   return (New_Shape);
  747. X   }
  748. X
  749. Xvoid Translate_CSG (Object, Vector)
  750. X   OBJECT *Object;
  751. X   VECTOR *Vector;
  752. X   {
  753. X   SHAPE *Local_Shape;
  754. X
  755. X   for (Local_Shape = ((CSG_SHAPE *) Object) -> Shapes;
  756. X        Local_Shape != NULL ;
  757. X        Local_Shape = Local_Shape -> Next_Object)
  758. X
  759. X      Translate ((OBJECT *) Local_Shape, Vector);   
  760. X   }
  761. X
  762. Xvoid Rotate_CSG (Object, Vector)
  763. X   OBJECT *Object;
  764. X   VECTOR *Vector;
  765. X   {
  766. X   SHAPE *Local_Shape;
  767. X
  768. X   for (Local_Shape = ((CSG_SHAPE *) Object) -> Shapes;
  769. X        Local_Shape != NULL ;
  770. X        Local_Shape = Local_Shape -> Next_Object)
  771. X
  772. X      Rotate ((OBJECT *) Local_Shape, Vector);   
  773. X   }
  774. X
  775. Xvoid Scale_CSG (Object, Vector)
  776. X   OBJECT *Object;
  777. X   VECTOR *Vector;
  778. X   {
  779. X   SHAPE *Local_Shape;
  780. X
  781. X   for (Local_Shape = ((CSG_SHAPE *) Object) -> Shapes;
  782. X        Local_Shape != NULL ;
  783. X        Local_Shape = Local_Shape -> Next_Object)
  784. X
  785. X      Scale ((OBJECT *) Local_Shape, Vector);   
  786. X   }
  787. X
  788. Xvoid Invert_CSG (Object)
  789. X   OBJECT *Object;
  790. X   {
  791. X   SHAPE *Local_Shape;
  792. X   CSG_SHAPE *Csg = (CSG_SHAPE *) Object;
  793. X
  794. X   if (Csg->Type == CSG_INTERSECTION_TYPE) {
  795. X      Csg->Type = CSG_UNION_TYPE;
  796. X      Csg->Methods = &CSG_Union_Methods;
  797. X      }
  798. X   else if (Csg->Type == CSG_UNION_TYPE) {
  799. X      Csg->Type = CSG_INTERSECTION_TYPE;
  800. X      Csg->Methods = &CSG_Intersection_Methods;
  801. X      }
  802. X
  803. X   for (Local_Shape = Csg -> Shapes;
  804. X        Local_Shape != NULL ;
  805. X        Local_Shape = Local_Shape -> Next_Object)
  806. X
  807. X      Invert ((OBJECT *) Local_Shape);   
  808. X   }
  809. X
  810. Xvoid Set_CSG_Parents (Shape, Object)
  811. X   CSG_SHAPE *Shape;
  812. X   OBJECT *Object;
  813. X   {
  814. X   SHAPE *Local_Shape;
  815. X
  816. X   for (Local_Shape = Shape -> Shapes;
  817. X        Local_Shape != NULL ;
  818. X        Local_Shape = Local_Shape -> Next_Object) {
  819. X
  820. X      Local_Shape->Parent_Object = Object;
  821. X      if ((Local_Shape->Type == CSG_UNION_TYPE) ||
  822. X          (Local_Shape->Type == CSG_INTERSECTION_TYPE))
  823. X         Set_CSG_Parents((CSG_SHAPE *)Local_Shape, Object);
  824. X      }
  825. X   }
  826. X
  827. END_OF_FILE
  828. if test 7895 -ne `wc -c <'src/csg.c'`; then
  829.     echo shar: \"'src/csg.c'\" unpacked with wrong size!
  830. fi
  831. # end of 'src/csg.c'
  832. fi
  833. if test -f 'src/gif.c' -a "${1}" != "-c" ; then 
  834.   echo shar: Will not clobber existing file \"'src/gif.c'\"
  835. else
  836. echo shar: Extracting \"'src/gif.c'\" \(7511 characters\)
  837. sed "s/^X//" >'src/gif.c' <<'END_OF_FILE'
  838. X/*****************************************************************************
  839. X*
  840. X*                                     gif.c
  841. X*
  842. X*   from DKBTrace (c) 1990  David Buck
  843. X*
  844. X*  Gif-format file reader.
  845. X*
  846. X*  NOTE:  Portions of this module were written by Steve Bennett and are used
  847. X*         here with his permission.
  848. X*
  849. X* This software is freely distributable. The source and/or object code may be
  850. X* copied or uploaded to communications services so long as this notice remains
  851. X* at the top of each file.  If any changes are made to the program, you must
  852. X* clearly indicate in the documentation and in the programs startup message
  853. X* who it was who made the changes. The documentation should also describe what
  854. X* those changes were. This software may not be included in whole or in
  855. X* part into any commercial package without the express written consent of the
  856. X* author.  It may, however, be included in other public domain or freely
  857. X* distributed software so long as the proper credit for the software is given.
  858. X*
  859. X* This software is provided as is without any guarantees or warranty. Although
  860. X* the author has attempted to find and correct any bugs in the software, he
  861. X* is not responsible for any damage caused by the use of the software.  The
  862. X* author is under no obligation to provide service, corrections, or upgrades
  863. X* to this package.
  864. X*
  865. X* Despite all the legal stuff above, if you do find bugs, I would like to hear
  866. X* about them.  Also, if you have any comments or questions, you may contact me
  867. X* at the following address:
  868. X*
  869. X*     David Buck
  870. X*     22C Sonnet Cres.
  871. X*     Nepean Ontario
  872. X*     Canada, K2H 8W7
  873. X*
  874. X*  I can also be reached on the following bulleton boards:
  875. X*
  876. X*     ATX              (613) 526-4141
  877. X*     OMX              (613) 731-3419
  878. X*     Mystic           (613) 731-0088 or (613) 731-6698
  879. X*
  880. X*  Fidonet:   1:163/109.9
  881. X*  Internet:  David_Buck@Carleton.CA
  882. X*
  883. X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  884. X*
  885. X*     Lattice BBS                      (708) 916-1200
  886. X*     The Information Exchange BBS     (708) 945-5575
  887. X*     Stillwaters BBS                  (708) 403-2826
  888. X*
  889. X*****************************************************************************/
  890. X
  891. X/*
  892. X   The following routines were borrowed freely from FRACTINT, and represent
  893. X   a generalized GIF file decoder.  This seems the best, most universal format
  894. X   for reading in Bitmapped images.  GIF is a Copyright of Compuserve, Inc.
  895. X   Swiped and converted to entirely "C" coded routines by AAC for the most
  896. X   in future portability!
  897. X*/
  898. X
  899. X#include "frame.h"
  900. X#include "dkbproto.h"
  901. X
  902. Xstatic IMAGE *Current_Image;  
  903. Xstatic int Bitmap_Line;
  904. Xstatic FILE *Bit_File;
  905. Xunsigned char *decoderline  /*  [2049] */ ;  /* write-line routines use this */
  906. X
  907. Xstatic IMAGE_COLOUR *gif_colour_map;
  908. Xstatic int colourmap_size;
  909. X
  910. Xint out_line (pixels, linelen)
  911. X   unsigned char *pixels;
  912. X   int linelen;
  913. X   {
  914. X   register int x;
  915. X   register unsigned addr;
  916. X
  917. X   addr = linelen * Bitmap_Line--;
  918. X
  919. X   for (x = 0; x < linelen; x++) {
  920. X      Current_Image->red[addr+x] = gif_colour_map[*pixels].Red;
  921. X      Current_Image->green[addr+x] = gif_colour_map[*pixels].Green;
  922. X      Current_Image->blue[addr+x] = gif_colour_map[*pixels].Blue;
  923. X      pixels++;
  924. X      }
  925. X
  926. X   return (0);
  927. X   }
  928. X
  929. X#define READ_ERROR -1
  930. X
  931. Xint get_byte() /* get byte from file, return the next byte or an error */
  932. X   {
  933. X   register int byte;
  934. X
  935. X   if ((byte = getc(Bit_File)) != EOF)
  936. X      return (byte);
  937. X   else {
  938. X      printf ("Premature end of file reading GIF image\n");
  939. X      exit (1);
  940. X      }
  941. X   return (0);  /* Keep the compiler happy */
  942. X   }
  943. X
  944. X/* Main GIF file decoder.  */
  945. X
  946. Xvoid read_gif_image(Image, filename)
  947. X   IMAGE *Image;
  948. X   char *filename;
  949. X   {
  950. X   register int i, j, status;
  951. X   unsigned finished, planes;
  952. X   unsigned char buffer[16];
  953. X
  954. X   status = 0;
  955. X   Current_Image = Image;
  956. X
  957. X   if ((Bit_File = fopen(filename, "rb")) == NULL) {
  958. X      printf ("Cannot open GIF file %s\n", filename);
  959. X      exit(1);
  960. X      }
  961. X
  962. X   /* zero out the full write-line */
  963. X   if ((decoderline = (unsigned char *) malloc (2049)) == NULL) {
  964. X      printf ("Cannot allocate space for gif decoder line\n");
  965. X      fclose (Bit_File);
  966. X      exit (1);
  967. X      }
  968. X
  969. X   for (i = 0; i < 2049; i++)
  970. X      decoderline[i] = (unsigned char) 0;
  971. X
  972. X   /* Get the screen description */
  973. X   for (i = 0; i < 13; i++)
  974. X      buffer[i] = (unsigned char)get_byte();
  975. X
  976. X   if (strncmp(buffer,"GIF87a",3) ||          /* use updated GIF specs */
  977. X       buffer[3] < '0' || buffer[3] > '9' ||
  978. X       buffer[4] < '0' || buffer[4] > '9' ||
  979. X       buffer[5] < 'A' || buffer[5] > 'z' ) {
  980. X
  981. X      printf ("Invalid GIF file format: %s\n", filename);
  982. X      fclose(Bit_File);
  983. X      exit (1);
  984. X      }
  985. X
  986. X   planes = ((unsigned)buffer[10] & 0x0F) + 1;
  987. X   colourmap_size = (int)(1 << planes);
  988. X   if ((gif_colour_map = (IMAGE_COLOUR *)
  989. X         malloc (colourmap_size * sizeof (IMAGE_COLOUR))) == NULL) {
  990. X      printf ("Cannot allocate gif colour map\n");
  991. X      fclose (Bit_File);
  992. X      exit (1);
  993. X      }
  994. X
  995. X   if ((buffer[10] & 0x80) == 0) {    /* color map (better be!) */
  996. X      printf ("Invalid GIF file format: %s\n", filename);
  997. X      fclose(Bit_File);
  998. X      exit (1);
  999. X      }
  1000. X
  1001. X   for (i = 0; i < colourmap_size ; i++) {
  1002. X      gif_colour_map[i].Red = (unsigned char)get_byte();
  1003. X      gif_colour_map[i].Green = (unsigned char)get_byte();
  1004. X      gif_colour_map[i].Blue = (unsigned char)get_byte();
  1005. X      }
  1006. X
  1007. X /* Now display one or more GIF objects */
  1008. X   finished = FALSE;
  1009. X   while (!finished) {
  1010. X      switch (get_byte()) {
  1011. X         case ';':                /* End of the GIF dataset */
  1012. X            finished = TRUE;
  1013. X            status = 0;
  1014. X            break;
  1015. X
  1016. X         case '!':                /* GIF Extension Block */
  1017. X            get_byte();           /* read (and ignore) the ID */
  1018. X            while ((i = get_byte()) > 0) /* get data len*/
  1019. X            for (j = 0; j < i; j++)
  1020. X                get_byte(); /* flush data */
  1021. X            break;
  1022. X
  1023. X         case ',': /* Start of image object. get description */
  1024. X            for (i = 0; i < 9; i++) {
  1025. X               if ((buffer[i] = (unsigned char)get_byte()) < 0) {
  1026. X                  status = -1;
  1027. X                  break;
  1028. X                  }
  1029. X               }
  1030. X
  1031. X            if (status < 0) {
  1032. X               finished = TRUE;
  1033. X               break;
  1034. X               }
  1035. X
  1036. X            Image->iwidth  = buffer[4] | (buffer[5] << 8);
  1037. X            Image->iheight = buffer[6] | (buffer[7] << 8);
  1038. X            Image->width = (DBL) Image->iwidth;
  1039. X            Image->height = (DBL) Image->iheight;
  1040. X
  1041. X            if (Image->iwidth > BITMAP_X_SIZE ||
  1042. X                Image->iheight > BITMAP_Y_SIZE) {
  1043. X               printf("\nBitmap File TOO BIG!!\n");
  1044. X               fclose(Bit_File);
  1045. X               exit (1);
  1046. X               }
  1047. X
  1048. X            Bitmap_Line = Image->iheight - 1;
  1049. X
  1050. X            if (((Image->red = (unsigned char *) malloc(Image->iwidth*Image->iheight))==NULL) ||
  1051. X                ((Image->green = (unsigned char *) malloc(Image->iwidth*Image->iheight))==NULL) ||
  1052. X                ((Image->blue = (unsigned char *) malloc(Image->iwidth*Image->iheight))==NULL)) {
  1053. X               printf ("Cannot allocate memory for picture\n");
  1054. X               exit(1);
  1055. X               }
  1056. X
  1057. X          /* Setup the color palette for the image */
  1058. X            status = decoder ((short) Image->iwidth); /*put bytes in Buf*/
  1059. X            finished = TRUE;
  1060. X            break;
  1061. X
  1062. X         default:
  1063. X            status = -1;
  1064. X            finished = TRUE;
  1065. X            break;
  1066. X         }
  1067. X      }
  1068. X
  1069. X   free (decoderline);
  1070. X   free (gif_colour_map);
  1071. X   fclose(Bit_File);
  1072. X   }
  1073. END_OF_FILE
  1074. if test 7511 -ne `wc -c <'src/gif.c'`; then
  1075.     echo shar: \"'src/gif.c'\" unpacked with wrong size!
  1076. fi
  1077. # end of 'src/gif.c'
  1078. fi
  1079. if test -f 'src/iffw.c' -a "${1}" != "-c" ; then 
  1080.   echo shar: Will not clobber existing file \"'src/iffw.c'\"
  1081. else
  1082. echo shar: Extracting \"'src/iffw.c'\" \(7573 characters\)
  1083. sed "s/^X//" >'src/iffw.c' <<'END_OF_FILE'
  1084. X/*----------------------------------------------------------------------*
  1085. X * IFFW.C  Support routines for writing IFF-85 files.          1/23/86
  1086. X * (IFF is Interchange Format File.)
  1087. X *
  1088. X * By Jerry Morrison and Steve Shaw, Electronic Arts.
  1089. X * This software is in the public domain.
  1090. X *
  1091. X * This version for the Commodore-Amiga computer.
  1092. X *----------------------------------------------------------------------*/
  1093. X#include "iff/iff.h"
  1094. X#include "iff/gio.h"
  1095. X
  1096. X/* ---------- IFF Writer -----------------------------------------------*/
  1097. X
  1098. X/* A macro to test if a chunk size is definite, i.e. not szNotYetKnown.*/
  1099. X#define Known(size)   ( (size) != szNotYetKnown )
  1100. X
  1101. X/* Yet another weird macro to make the source code simpler...*/
  1102. X#define IfIffp(expr)  {if (iffp == IFF_OKAY)  iffp = (expr);}
  1103. X
  1104. X/* ---------- OpenWIFF -------------------------------------------------*/
  1105. X
  1106. XIFFP OpenWIFF(file, new0, limit)  BPTR file; GroupContext *new0; LONG limit; {
  1107. X    register GroupContext *new = new0;
  1108. X    register IFFP iffp = IFF_OKAY;
  1109. X
  1110. X    new->parent       = NULL;
  1111. X    new->clientFrame  = NULL;
  1112. X    new->file         = file;
  1113. X    new->position     = 0;
  1114. X    new->bound        = limit;
  1115. X    new->ckHdr.ckID   = NULL_CHUNK;  /* indicates no current chunk */
  1116. X    new->ckHdr.ckSize = new->bytesSoFar = 0;
  1117. X
  1118. X    if (0 > Seek(file, 0, OFFSET_BEGINNING))    /* Go to start of the file.*/
  1119. X    iffp = DOS_ERROR;
  1120. X    else if ( Known(limit) && IS_ODD(limit) )
  1121. X    iffp = CLIENT_ERROR;
  1122. X    return(iffp);
  1123. X    }
  1124. X
  1125. X/* ---------- StartWGroup ----------------------------------------------*/
  1126. XIFFP StartWGroup(parent, groupType, groupSize, subtype, new)
  1127. X      GroupContext *parent, *new; ID groupType, subtype; LONG groupSize;  {
  1128. X    register IFFP iffp;
  1129. X
  1130. X    iffp = PutCkHdr(parent, groupType, groupSize);
  1131. X    IfIffp( IFFWriteBytes(parent, (BYTE *)&subtype, sizeof(ID)) );
  1132. X    IfIffp( OpenWGroup(parent, new) );
  1133. X    return(iffp);
  1134. X    }
  1135. X
  1136. X/* ---------- OpenWGroup -----------------------------------------------*/
  1137. XIFFP OpenWGroup(parent0, new0)  GroupContext *parent0, *new0; {
  1138. X    register GroupContext *parent = parent0;
  1139. X    register GroupContext *new    = new0;
  1140. X    register LONG ckEnd;
  1141. X    register IFFP iffp = IFF_OKAY;
  1142. X
  1143. X    new->parent       = parent;
  1144. X    new->clientFrame  = parent->clientFrame;
  1145. X    new->file         = parent->file;
  1146. X    new->position     = parent->position;
  1147. X    new->bound        = parent->bound;
  1148. X    new->ckHdr.ckID   = NULL_CHUNK;
  1149. X    new->ckHdr.ckSize = new->bytesSoFar = 0;
  1150. X
  1151. X    if ( Known(parent->ckHdr.ckSize) ) {
  1152. X    ckEnd = new->position + ChunkMoreBytes(parent);
  1153. X    if ( new->bound == szNotYetKnown || new->bound > ckEnd )
  1154. X        new->bound = ckEnd;
  1155. X    };
  1156. X
  1157. X    if ( parent->ckHdr.ckID == NULL_CHUNK || /* not currently writing a chunk*/
  1158. X     IS_ODD(new->position) ||
  1159. X         (Known(new->bound) && IS_ODD(new->bound)) )
  1160. X    iffp = CLIENT_ERROR;
  1161. X    return(iffp);
  1162. X    }
  1163. X
  1164. X/* ---------- CloseWGroup ----------------------------------------------*/
  1165. XIFFP CloseWGroup(old0)  GroupContext *old0; {
  1166. X    register GroupContext *old = old0;
  1167. X    IFFP iffp = IFF_OKAY;
  1168. X
  1169. X    if ( old->ckHdr.ckID != NULL_CHUNK )  /* didn't close the last chunk */
  1170. X    iffp = CLIENT_ERROR;
  1171. X    else if ( old->parent == NULL ) {      /* top level file context */
  1172. X    if (GWriteFlush(old->file) < 0)  iffp = DOS_ERROR;
  1173. X    }
  1174. X    else {                  /* update parent context */
  1175. X    old->parent->bytesSoFar += old->position - old->parent->position;
  1176. X    old->parent->position = old->position;
  1177. X    };
  1178. X    return(iffp);
  1179. X    }
  1180. X
  1181. X/* ---------- EndWGroup ------------------------------------------------*/
  1182. XIFFP EndWGroup(old)  GroupContext *old;  {
  1183. X    register GroupContext *parent = old->parent;
  1184. X    register IFFP iffp;
  1185. X
  1186. X    iffp = CloseWGroup(old);
  1187. X    IfIffp( PutCkEnd(parent) );
  1188. X    return(iffp);
  1189. X    }
  1190. X
  1191. X/* ---------- PutCk ----------------------------------------------------*/
  1192. XIFFP PutCk(context, ckID, ckSize, data)
  1193. X      GroupContext *context; ID ckID; LONG ckSize; BYTE *data; {
  1194. X    register IFFP iffp = IFF_OKAY;
  1195. X
  1196. X    if ( ckSize == szNotYetKnown )
  1197. X    iffp = CLIENT_ERROR;
  1198. X    IfIffp( PutCkHdr(context, ckID, ckSize) );
  1199. X    IfIffp( IFFWriteBytes(context, data, ckSize) );
  1200. X    IfIffp( PutCkEnd(context) );
  1201. X    return(iffp);
  1202. X    }
  1203. X
  1204. X/* ---------- PutCkHdr -------------------------------------------------*/
  1205. XIFFP PutCkHdr(context0, ckID, ckSize)
  1206. X      GroupContext *context0;  ID ckID;  LONG ckSize; {
  1207. X    register GroupContext *context = context0;
  1208. X    LONG minPSize = sizeof(ChunkHeader); /* physical chunk >= minPSize bytes*/
  1209. X
  1210. X    /* CLIENT_ERROR if we're already inside a chunk or asked to write
  1211. X     * other than one FORM, LIST, or CAT at the top level of a file */
  1212. X    /* Also, non-positive ID values are illegal and used for error codes.*/
  1213. X    /* (We could check for other illegal IDs...)*/
  1214. X    if ( context->ckHdr.ckID != NULL_CHUNK  ||  ckID <= 0 )
  1215. X    return(CLIENT_ERROR);
  1216. X    else if (context->parent == NULL)  {
  1217. X    switch (ckID)  {
  1218. X        case FORM:  case LIST:  case CAT:  break;
  1219. X        default: return(CLIENT_ERROR);
  1220. X        }
  1221. X    if (context->position != 0)
  1222. X        return(CLIENT_ERROR);
  1223. X    }
  1224. X
  1225. X    if ( Known(ckSize) ) {
  1226. X    if ( ckSize < 0 )
  1227. X        return(CLIENT_ERROR);
  1228. X    minPSize += ckSize;
  1229. X    };
  1230. X    if ( Known(context->bound)  &&
  1231. X         context->position + minPSize > context->bound )
  1232. X    return(CLIENT_ERROR);
  1233. X
  1234. X    context->ckHdr.ckID   = ckID;
  1235. X    context->ckHdr.ckSize = ckSize;
  1236. X    context->bytesSoFar   = 0;
  1237. X    if (0 >
  1238. X    GWrite(context->file, (BYTE *)&context->ckHdr, sizeof(ChunkHeader))
  1239. X    )
  1240. X    return(DOS_ERROR);
  1241. X    context->position += sizeof(ChunkHeader);
  1242. X    return(IFF_OKAY);
  1243. X    }
  1244. X
  1245. X/* ---------- IFFWriteBytes ---------------------------------------------*/
  1246. XIFFP IFFWriteBytes(context0, data, nBytes)
  1247. X      GroupContext *context0;  BYTE *data;  LONG nBytes; {
  1248. X    register GroupContext *context = context0;
  1249. X
  1250. X    if ( context->ckHdr.ckID == NULL_CHUNK  ||    /* not in a chunk */
  1251. X     nBytes < 0  ||                /* negative nBytes */
  1252. X     (Known(context->bound)  &&        /* overflow context */
  1253. X        context->position + nBytes > context->bound)  ||
  1254. X     (Known(context->ckHdr.ckSize)  &&       /* overflow chunk */
  1255. X        context->bytesSoFar + nBytes > context->ckHdr.ckSize) )
  1256. X    return(CLIENT_ERROR);
  1257. X
  1258. X    if (0 > GWrite(context->file, data, nBytes))
  1259. X    return(DOS_ERROR);
  1260. X
  1261. X    context->bytesSoFar += nBytes;
  1262. X    context->position   += nBytes;
  1263. X    return(IFF_OKAY);
  1264. X    }
  1265. X
  1266. X/* ---------- PutCkEnd -------------------------------------------------*/
  1267. XIFFP PutCkEnd(context0)  GroupContext *context0; {
  1268. X    register GroupContext *context = context0;
  1269. X    WORD zero = 0;    /* padding source */
  1270. X
  1271. X    if ( context->ckHdr.ckID == NULL_CHUNK )  /* not in a chunk */
  1272. X    return(CLIENT_ERROR);
  1273. X
  1274. X    if ( context->ckHdr.ckSize == szNotYetKnown ) {
  1275. X    /* go back and set the chunk size to bytesSoFar */
  1276. X    if ( 0 >
  1277. XGSeek(context->file, -(context->bytesSoFar + sizeof(LONG)), OFFSET_CURRENT) ||
  1278. X         0 >
  1279. XGWrite(context->file, (BYTE *)&context->bytesSoFar, sizeof(LONG))  ||
  1280. X         0 >
  1281. XGSeek(context->file, context->bytesSoFar, OFFSET_CURRENT)  )
  1282. X        return(DOS_ERROR);
  1283. X    }
  1284. X    else {  /* make sure the client wrote as many bytes as planned */
  1285. X    if ( context->ckHdr.ckSize != context->bytesSoFar )
  1286. X        return(CLIENT_ERROR);
  1287. X    };
  1288. X
  1289. X    /* Write a pad byte if needed to bring us up to an even boundary.
  1290. X     * Since the context end must be even, and since we haven't
  1291. X     * overwritten the context, if we're on an odd position there must
  1292. X     * be room for a pad byte. */
  1293. X    if ( IS_ODD(context->bytesSoFar) ) {
  1294. X    if ( 0 > GWrite(context->file, (BYTE *)&zero, 1) )
  1295. X        return(DOS_ERROR);
  1296. X    context->position += 1;
  1297. X    };
  1298. X
  1299. X    context->ckHdr.ckID   = NULL_CHUNK;
  1300. X    context->ckHdr.ckSize = context->bytesSoFar = 0;
  1301. X    return(IFF_OKAY);
  1302. X    }
  1303. X
  1304. END_OF_FILE
  1305. if test 7573 -ne `wc -c <'src/iffw.c'`; then
  1306.     echo shar: \"'src/iffw.c'\" unpacked with wrong size!
  1307. fi
  1308. # end of 'src/iffw.c'
  1309. fi
  1310. if test -f 'src/matrices.c' -a "${1}" != "-c" ; then 
  1311.   echo shar: Will not clobber existing file \"'src/matrices.c'\"
  1312. else
  1313. echo shar: Extracting \"'src/matrices.c'\" \(9507 characters\)
  1314. sed "s/^X//" >'src/matrices.c' <<'END_OF_FILE'
  1315. X/*****************************************************************************
  1316. X*
  1317. X*                                   matrices.c
  1318. X*
  1319. X*   from DKBTrace (c) 1990  David Buck
  1320. X*
  1321. X*  This module contains code to manipulate 4x4 matrices.
  1322. X*
  1323. X* This software is freely distributable. The source and/or object code may be
  1324. X* copied or uploaded to communications services so long as this notice remains
  1325. X* at the top of each file.  If any changes are made to the program, you must
  1326. X* clearly indicate in the documentation and in the programs startup message
  1327. X* who it was who made the changes. The documentation should also describe what
  1328. X* those changes were. This software may not be included in whole or in
  1329. X* part into any commercial package without the express written consent of the
  1330. X* author.  It may, however, be included in other public domain or freely
  1331. X* distributed software so long as the proper credit for the software is given.
  1332. X*
  1333. X* This software is provided as is without any guarantees or warranty. Although
  1334. X* the author has attempted to find and correct any bugs in the software, he
  1335. X* is not responsible for any damage caused by the use of the software.  The
  1336. X* author is under no obligation to provide service, corrections, or upgrades
  1337. X* to this package.
  1338. X*
  1339. X* Despite all the legal stuff above, if you do find bugs, I would like to hear
  1340. X* about them.  Also, if you have any comments or questions, you may contact me
  1341. X* at the following address:
  1342. X*
  1343. X*     David Buck
  1344. X*     22C Sonnet Cres.
  1345. X*     Nepean Ontario
  1346. X*     Canada, K2H 8W7
  1347. X*
  1348. X*  I can also be reached on the following bulleton boards:
  1349. X*
  1350. X*     ATX              (613) 526-4141
  1351. X*     OMX              (613) 731-3419
  1352. X*     Mystic           (613) 731-0088 or (613) 731-6698
  1353. X*
  1354. X*  Fidonet:   1:163/109.9
  1355. X*  Internet:  David_Buck@Carleton.CA
  1356. X*
  1357. X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  1358. X*
  1359. X*     Lattice BBS                      (708) 916-1200
  1360. X*     The Information Exchange BBS     (708) 945-5575
  1361. X*     Stillwaters BBS                  (708) 403-2826
  1362. X*
  1363. X*****************************************************************************/
  1364. X
  1365. X
  1366. X#include "frame.h"
  1367. X#include "vector.h"
  1368. X#include "dkbproto.h"
  1369. X
  1370. X#ifndef PI
  1371. X#define PI 3.141592653689793
  1372. X#endif
  1373. X
  1374. Xvoid MZero (result)
  1375. X   MATRIX *result;
  1376. X   {
  1377. X/* Initialize the matrix to the following values:
  1378. X   0.0   0.0   0.0   0.0
  1379. X   0.0   0.0   0.0   0.0
  1380. X   0.0   0.0   0.0   0.0
  1381. X   0.0   0.0   0.0   0.0
  1382. X*/
  1383. X   register int i, j;
  1384. X
  1385. X   for (i = 0 ; i < 4 ; i++)
  1386. X      for (j = 0 ; j < 4 ; j++)
  1387. X         (*result)[i][j] = 0.0;
  1388. X   }
  1389. X
  1390. Xvoid MIdentity (result)
  1391. X   MATRIX *result;
  1392. X   {
  1393. X/* Initialize the matrix to the following values:
  1394. X   1.0   0.0   0.0   0.0
  1395. X   0.0   1.0   0.0   0.0
  1396. X   0.0   0.0   1.0   0.0
  1397. X   0.0   0.0   0.0   1.0
  1398. X*/
  1399. X   register int i, j;
  1400. X
  1401. X   for (i = 0 ; i < 4 ; i++)
  1402. X     for (j = 0 ; j < 4 ; j++)
  1403. X        if (i == j)
  1404. X           (*result)[i][j] = 1.0;
  1405. X        else
  1406. X           (*result)[i][j] = 0.0;
  1407. X   }
  1408. X
  1409. Xvoid MTimes (result, matrix1, matrix2)
  1410. X   MATRIX *result, *matrix1, *matrix2;
  1411. X   {
  1412. X   register int i, j, k;
  1413. X   MATRIX temp_matrix;
  1414. X
  1415. X   for (i = 0 ; i < 4 ; i++)
  1416. X      for (j = 0 ; j < 4 ; j++) {
  1417. X         temp_matrix[i][j] = 0.0;
  1418. X         for (k = 0 ; k < 4 ; k++)
  1419. X            temp_matrix[i][j] += (*matrix1)[i][k] * (*matrix2)[k][j];
  1420. X         }
  1421. X
  1422. X   for (i = 0 ; i < 4 ; i++)
  1423. X      for (j = 0 ; j < 4 ; j++)
  1424. X         (*result)[i][j] = temp_matrix[i][j];
  1425. X   }
  1426. X
  1427. X
  1428. Xvoid MAdd (result, matrix1, matrix2)
  1429. X   MATRIX *result, *matrix1, *matrix2;
  1430. X   {
  1431. X   register int i, j;
  1432. X
  1433. X   for (i = 0 ; i < 4 ; i++)
  1434. X      for (j = 0 ; j < 4 ; j++)
  1435. X         (*result)[i][j] = (*matrix1)[i][j] + (*matrix2)[i][j];
  1436. X   }
  1437. X
  1438. Xvoid MSub (result, matrix1, matrix2)
  1439. X   MATRIX *result, *matrix1, *matrix2;
  1440. X   {
  1441. X   register int i, j;
  1442. X
  1443. X   for (i = 0 ; i < 4 ; i++)
  1444. X      for (j = 0 ; j < 4 ; j++)
  1445. X         (*result)[i][j] = (*matrix1)[i][j] - (*matrix2)[i][j];
  1446. X   }
  1447. X
  1448. Xvoid MScale (result, matrix1, amount)
  1449. XMATRIX *result, *matrix1;
  1450. XDBL amount;
  1451. X{
  1452. X   register int i, j;
  1453. X
  1454. X   for (i = 0 ; i < 4 ; i++)
  1455. X      for (j = 0 ; j < 4 ; j++)
  1456. X     if (amount == 1.0)
  1457. X        (*result)[i][j] = (*matrix1)[i][j]; /* just copy */
  1458. X     else
  1459. X            (*result)[i][j] = (*matrix1)[i][j] * amount;
  1460. X   return;
  1461. X}
  1462. X
  1463. Xvoid MTranspose (result, matrix1)
  1464. X   MATRIX *result, *matrix1;
  1465. X   {
  1466. X   register int i, j;
  1467. X   MATRIX temp_matrix;
  1468. X
  1469. X   for (i = 0 ; i < 4 ; i++)
  1470. X      for (j = 0 ; j < 4 ; j++)
  1471. X         temp_matrix[i][j] = (*matrix1)[j][i];
  1472. X
  1473. X   for (i = 0 ; i < 4 ; i++)
  1474. X      for (j = 0 ; j < 4 ; j++)
  1475. X         (*result)[i][j] = temp_matrix[i][j];
  1476. X   }
  1477. X
  1478. X
  1479. Xvoid MTransformVector (result, vector, transformation)
  1480. X   VECTOR *result, *vector;
  1481. X   TRANSFORMATION *transformation;
  1482. X   {
  1483. X   register int i;
  1484. X   DBL answer_array[4];
  1485. X   MATRIX *matrix;
  1486. X
  1487. X   matrix = &transformation -> matrix;
  1488. X
  1489. X   for (i = 0 ; i < 4 ; i++)
  1490. X      answer_array[i] = vector -> x * (*matrix)[0][i]
  1491. X                      + vector -> y * (*matrix)[1][i]
  1492. X                      + vector -> z * (*matrix)[2][i]
  1493. X                      + (*matrix)[3][i];
  1494. X
  1495. X   result -> x  = answer_array[0];
  1496. X   result -> y  = answer_array[1];
  1497. X   result -> z  = answer_array[2];
  1498. X   }
  1499. X
  1500. Xvoid MInverseTransformVector (result, vector, transformation)
  1501. X   VECTOR *result, *vector;
  1502. X   TRANSFORMATION *transformation;
  1503. X   {
  1504. X   register int i;
  1505. X   DBL answer_array[4];
  1506. X   MATRIX *matrix;
  1507. X
  1508. X   matrix = &transformation -> inverse;
  1509. X
  1510. X   for (i = 0 ; i < 4 ; i++)
  1511. X      answer_array[i] = vector -> x * (*matrix)[0][i]
  1512. X                      + vector -> y * (*matrix)[1][i]
  1513. X                      + vector -> z * (*matrix)[2][i]
  1514. X                      + (*matrix)[3][i];
  1515. X
  1516. X   result -> x  = answer_array[0];
  1517. X   result -> y  = answer_array[1];
  1518. X   result -> z  = answer_array[2];
  1519. X   }
  1520. X
  1521. Xvoid Get_Scaling_Transformation (result, vector)
  1522. X   TRANSFORMATION *result;
  1523. X   VECTOR *vector;
  1524. X   {
  1525. X   MIdentity (&result -> matrix);
  1526. X   (result -> matrix)[0][0] = vector -> x;
  1527. X   (result -> matrix)[1][1] = vector -> y;
  1528. X   (result -> matrix)[2][2] = vector -> z;
  1529. X
  1530. X   MIdentity (&result -> inverse);
  1531. X   (result -> inverse)[0][0] = 1.0 / vector -> x;
  1532. X   (result -> inverse)[1][1]= 1.0 / vector -> y;
  1533. X   (result -> inverse)[2][2] = 1.0 / vector -> z;
  1534. X   }
  1535. X
  1536. Xvoid Get_Inversion_Transformation (result)
  1537. X   TRANSFORMATION *result;
  1538. X   {
  1539. X   MIdentity (&result -> matrix);
  1540. X   (result -> matrix)[0][0] = -1.0;
  1541. X   (result -> matrix)[1][1] = -1.0;
  1542. X   (result -> matrix)[2][2] = -1.0;
  1543. X   (result -> matrix)[3][3] = -1.0;
  1544. X
  1545. X
  1546. X   (result -> inverse)[0][0] = -1.0;
  1547. X   (result -> inverse)[1][1] = -1.0;
  1548. X   (result -> inverse)[2][2] = -1.0;
  1549. X   (result -> inverse)[3][3] = -1.0;
  1550. X   }
  1551. X
  1552. Xvoid Get_Translation_Transformation (transformation, vector)
  1553. X   TRANSFORMATION *transformation;
  1554. X   VECTOR *vector;
  1555. X   {
  1556. X   MIdentity (&transformation -> matrix);
  1557. X   (transformation -> matrix)[3][0] = vector -> x;
  1558. X   (transformation -> matrix)[3][1] = vector -> y;
  1559. X   (transformation -> matrix)[3][2] = vector -> z;
  1560. X
  1561. X   MIdentity (&transformation -> inverse);
  1562. X   (transformation -> inverse)[3][0] = 0.0 - vector -> x;
  1563. X   (transformation -> inverse)[3][1] = 0.0 - vector -> y;
  1564. X   (transformation -> inverse)[3][2] = 0.0 - vector -> z;
  1565. X   }
  1566. X
  1567. Xvoid Get_Rotation_Transformation (transformation, vector)
  1568. X   TRANSFORMATION *transformation;
  1569. X   VECTOR *vector;
  1570. X   {
  1571. X   MATRIX Matrix;
  1572. X   VECTOR Radian_Vector;
  1573. X   register DBL cosx, cosy, cosz, sinx, siny, sinz;
  1574. X
  1575. X   VScale (Radian_Vector, *vector, PI/180.0);
  1576. X   MIdentity (&transformation -> matrix);
  1577. X   cosx = cos (Radian_Vector.x);
  1578. X   sinx = sin (Radian_Vector.x);
  1579. X   cosy = cos (Radian_Vector.y);
  1580. X   siny = sin (Radian_Vector.y);
  1581. X   cosz = cos (Radian_Vector.z);
  1582. X   sinz = sin (Radian_Vector.z);
  1583. X
  1584. X   (transformation -> matrix) [1][1] = cosx;
  1585. X   (transformation -> matrix) [2][2] = cosx;
  1586. X   (transformation -> matrix) [1][2] = 0.0 - sinx;
  1587. X   (transformation -> matrix) [2][1] = sinx;
  1588. X   MTranspose (&transformation -> inverse, &transformation -> matrix);
  1589. X
  1590. X   MIdentity (&Matrix);
  1591. X   Matrix [0][0] = cosy;
  1592. X   Matrix [2][2] = cosy;
  1593. X   Matrix [0][2] = siny;
  1594. X   Matrix [2][0] = 0.0 - siny;
  1595. X   MTimes (&transformation -> matrix, &transformation -> matrix, &Matrix);
  1596. X   MTranspose (&Matrix, &Matrix);
  1597. X   MTimes (&transformation -> inverse, &Matrix, &transformation -> inverse);
  1598. X
  1599. X   MIdentity (&Matrix);
  1600. X   Matrix [0][0] = cosz;
  1601. X   Matrix [1][1] = cosz;
  1602. X   Matrix [0][1] = 0.0 - sinz;
  1603. X   Matrix [1][0] = sinz;
  1604. X   MTimes (&transformation -> matrix, &transformation -> matrix, &Matrix);
  1605. X   MTranspose (&Matrix, &Matrix);
  1606. X   MTimes (&transformation -> inverse, &Matrix, &transformation -> inverse);
  1607. X   }
  1608. X
  1609. Xvoid Get_Look_At_Transformation (result, Look_At, Up, Right)
  1610. X   TRANSFORMATION *result;
  1611. X   VECTOR *Look_At, *Up, *Right;
  1612. X   {
  1613. X   MIdentity (&result -> inverse);
  1614. X   (result -> matrix)[0][0] = Right->x;
  1615. X   (result -> matrix)[0][1] = Right->y;
  1616. X   (result -> matrix)[0][2] = Right->z;
  1617. X   (result -> matrix)[1][0] = Up->x;
  1618. X   (result -> matrix)[1][1] = Up->y;
  1619. X   (result -> matrix)[1][2] = Up->z;
  1620. X   (result -> matrix)[2][0] = Look_At->x;
  1621. X   (result -> matrix)[2][1] = Look_At->y;
  1622. X   (result -> matrix)[2][2] = Look_At->z;
  1623. X
  1624. X   MIdentity (&result -> matrix);
  1625. X   MTranspose (&result -> matrix, &result -> inverse);   
  1626. X   }
  1627. X
  1628. Xvoid Compose_Transformations (Original_Transformation, New_Transformation)
  1629. X   TRANSFORMATION *Original_Transformation, *New_Transformation;
  1630. X   {
  1631. X   MTimes (&Original_Transformation -> matrix,
  1632. X           &Original_Transformation -> matrix,
  1633. X           &New_Transformation -> matrix);
  1634. X
  1635. X   MTimes (&Original_Transformation -> inverse,
  1636. X           &New_Transformation -> inverse,
  1637. X           &Original_Transformation -> inverse);
  1638. X   }
  1639. END_OF_FILE
  1640. if test 9507 -ne `wc -c <'src/matrices.c'`; then
  1641.     echo shar: \"'src/matrices.c'\" unpacked with wrong size!
  1642. fi
  1643. # end of 'src/matrices.c'
  1644. fi
  1645. if test -f 'src/planes.c' -a "${1}" != "-c" ; then 
  1646.   echo shar: Will not clobber existing file \"'src/planes.c'\"
  1647. else
  1648. echo shar: Extracting \"'src/planes.c'\" \(6704 characters\)
  1649. sed "s/^X//" >'src/planes.c' <<'END_OF_FILE'
  1650. X/*****************************************************************************
  1651. X*
  1652. X*                                    planes.c
  1653. X*
  1654. X*   from DKBTrace (c) 1990  David Buck
  1655. X*
  1656. X*  This module implements functions that manipulate planes.
  1657. X*
  1658. X* This software is freely distributable. The source and/or object code may be
  1659. X* copied or uploaded to communications services so long as this notice remains
  1660. X* at the top of each file.  If any changes are made to the program, you must
  1661. X* clearly indicate in the documentation and in the programs startup message
  1662. X* who it was who made the changes. The documentation should also describe what
  1663. X* those changes were. This software may not be included in whole or in
  1664. X* part into any commercial package without the express written consent of the
  1665. X* author.  It may, however, be included in other public domain or freely
  1666. X* distributed software so long as the proper credit for the software is given.
  1667. X*
  1668. X* This software is provided as is without any guarantees or warranty. Although
  1669. X* the author has attempted to find and correct any bugs in the software, he
  1670. X* is not responsible for any damage caused by the use of the software.  The
  1671. X* author is under no obligation to provide service, corrections, or upgrades
  1672. X* to this package.
  1673. X*
  1674. X* Despite all the legal stuff above, if you do find bugs, I would like to hear
  1675. X* about them.  Also, if you have any comments or questions, you may contact me
  1676. X* at the following address:
  1677. X*
  1678. X*     David Buck
  1679. X*     22C Sonnet Cres.
  1680. X*     Nepean Ontario
  1681. X*     Canada, K2H 8W7
  1682. X*
  1683. X*  I can also be reached on the following bulleton boards:
  1684. X*
  1685. X*     ATX              (613) 526-4141
  1686. X*     OMX              (613) 731-3419
  1687. X*     Mystic           (613) 731-0088 or (613) 731-6698
  1688. X*
  1689. X*  Fidonet:   1:163/109.9
  1690. X*  Internet:  David_Buck@Carleton.CA
  1691. X*
  1692. X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  1693. X*
  1694. X*     Lattice BBS                      (708) 916-1200
  1695. X*     The Information Exchange BBS     (708) 945-5575
  1696. X*     Stillwaters BBS                  (708) 403-2826
  1697. X*
  1698. X*****************************************************************************/
  1699. X
  1700. X
  1701. X#include "frame.h"
  1702. X#include "vector.h"
  1703. X#include "dkbproto.h"
  1704. X
  1705. XMETHODS Plane_Methods =
  1706. X   { Object_Intersect, All_Plane_Intersections,
  1707. X     Inside_Plane, Plane_Normal,
  1708. X     Copy_Plane,
  1709. X     Translate_Plane, Rotate_Plane,
  1710. X     Scale_Plane, Invert_Plane};
  1711. X
  1712. X#define Small_Tolerance 0.001
  1713. X
  1714. Xextern PLANE *Get_Plane_Shape();
  1715. X
  1716. Xextern RAY *VP_Ray;
  1717. Xextern long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  1718. X
  1719. Xint All_Plane_Intersections (Object, Ray, Depth_Queue)
  1720. X   OBJECT *Object;
  1721. X   RAY *Ray;
  1722. X   PRIOQ *Depth_Queue;
  1723. X   {
  1724. X   PLANE *Shape = (PLANE *) Object;
  1725. X   DBL Depth;
  1726. X   VECTOR Intersection_Point;
  1727. X   INTERSECTION Local_Element;
  1728. X
  1729. X   if (Intersect_Plane (Ray, Shape, &Depth))
  1730. X      if (Depth > Small_Tolerance)
  1731. X         {
  1732. X         Local_Element.Depth = Depth;
  1733. X         Local_Element.Object = Shape -> Parent_Object;
  1734. X         VScale (Intersection_Point, Ray -> Direction, Depth);
  1735. X         VAdd (Intersection_Point, Intersection_Point, Ray -> Initial);
  1736. X         Local_Element.Point = Intersection_Point;
  1737. X         Local_Element.Shape = (SHAPE *)Shape;
  1738. X         pq_add (Depth_Queue, &Local_Element);
  1739. X         return (TRUE);
  1740. X         }
  1741. X
  1742. X   return (FALSE);
  1743. X   }
  1744. X
  1745. Xint Intersect_Plane (Ray, Plane, Depth)
  1746. X   RAY *Ray;
  1747. X   PLANE *Plane;
  1748. X   DBL *Depth;
  1749. X   {
  1750. X   DBL NormalDotOrigin, NormalDotDirection;
  1751. X
  1752. X   Ray_Plane_Tests++;
  1753. X   if (Ray == VP_Ray) {
  1754. X      if (!Plane->VPCached) {
  1755. X         VDot (Plane->VPNormDotOrigin, Plane->Normal_Vector, Ray->Initial);
  1756. X         Plane->VPNormDotOrigin += Plane->Distance;
  1757. X         Plane->VPNormDotOrigin *= -1.0;
  1758. X         Plane->VPCached = TRUE;
  1759. X         }
  1760. X
  1761. X      VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  1762. X      if ((NormalDotDirection < Small_Tolerance) &&
  1763. X          (NormalDotDirection > -Small_Tolerance))
  1764. X         return (FALSE);
  1765. X
  1766. X      *Depth = Plane->VPNormDotOrigin / NormalDotDirection;
  1767. X      if ((*Depth >= Small_Tolerance) || (*Depth <= Max_Distance)) {
  1768. X         Ray_Plane_Tests_Succeeded++;
  1769. X         return (TRUE);
  1770. X         }
  1771. X      else
  1772. X         return (FALSE);
  1773. X      }
  1774. X   else {
  1775. X      VDot (NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
  1776. X      NormalDotOrigin += Plane->Distance;
  1777. X      NormalDotOrigin *= -1.0;
  1778. X
  1779. X      VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  1780. X      if ((NormalDotDirection < Small_Tolerance) &&
  1781. X          (NormalDotDirection > -Small_Tolerance))
  1782. X            return (FALSE);
  1783. X
  1784. X      *Depth = NormalDotOrigin / NormalDotDirection;
  1785. X      if ((*Depth >= Small_Tolerance) || (*Depth <= Max_Distance)) {
  1786. X         Ray_Plane_Tests_Succeeded++;
  1787. X         return (TRUE);
  1788. X         }
  1789. X      else
  1790. X         return (FALSE);
  1791. X      }
  1792. X   }
  1793. X
  1794. Xint Inside_Plane (Point, Object)
  1795. X   VECTOR *Point;
  1796. X   OBJECT *Object;
  1797. X   {
  1798. X   PLANE *Plane = (PLANE *) Object;
  1799. X   DBL Temp;
  1800. X
  1801. X   VDot (Temp, *Point, Plane->Normal_Vector);
  1802. X   return ((Temp + Plane->Distance) <= Small_Tolerance);
  1803. X   }
  1804. X
  1805. Xvoid Plane_Normal (Result, Object, Intersection_Point)
  1806. X   OBJECT *Object;
  1807. X   VECTOR *Result, *Intersection_Point;
  1808. X   {
  1809. X   PLANE *Plane = (PLANE *) Object;
  1810. X
  1811. X   *Result = Plane->Normal_Vector;
  1812. X   Perturb_Normal (Result, Plane->Parent_Object,
  1813. X                   Intersection_Point, Result);
  1814. X   }
  1815. X
  1816. Xvoid *Copy_Plane (Object)
  1817. X   OBJECT *Object;
  1818. X   {
  1819. X   PLANE *New_Shape;
  1820. X
  1821. X   New_Shape = Get_Plane_Shape ();
  1822. X   *New_Shape = * ((PLANE *)Object);
  1823. X   New_Shape -> Next_Object = NULL;
  1824. X   return (New_Shape);
  1825. X   }
  1826. X
  1827. Xvoid Translate_Plane (Object, Vector)
  1828. X   OBJECT *Object;
  1829. X   VECTOR *Vector;
  1830. X   {
  1831. X   PLANE *Plane = (PLANE *) Object;
  1832. X   VECTOR Translation;
  1833. X
  1834. X   VEvaluate (Translation, Plane->Normal_Vector, *Vector);
  1835. X   Plane->Distance -= Translation.x + Translation.y + Translation.z;
  1836. X   }
  1837. X
  1838. Xvoid Rotate_Plane (Object, Vector)
  1839. X   OBJECT *Object;
  1840. X   VECTOR *Vector;
  1841. X   {
  1842. X   TRANSFORMATION Transformation;
  1843. X
  1844. X   Get_Rotation_Transformation (&Transformation, Vector);
  1845. X   MTransformVector (&((PLANE *) Object)->Normal_Vector,
  1846. X                     &((PLANE *) Object)->Normal_Vector, &Transformation);
  1847. X   }
  1848. X
  1849. Xvoid Scale_Plane (Object, Vector)
  1850. X   OBJECT *Object;
  1851. X   VECTOR *Vector;
  1852. X   {
  1853. X   DBL Length;
  1854. X   PLANE *Plane = (PLANE  *) Object;
  1855. X
  1856. X   Plane->Normal_Vector.x = Plane->Normal_Vector.x / Vector->x;
  1857. X   Plane->Normal_Vector.y = Plane->Normal_Vector.y / Vector->y;
  1858. X   Plane->Normal_Vector.z = Plane->Normal_Vector.z / Vector->z;
  1859. X
  1860. X   VLength(Length, Plane->Normal_Vector);
  1861. X   VScale (Plane->Normal_Vector, Plane->Normal_Vector, 1.0 / Length);
  1862. X   Plane->Distance /= Length;
  1863. X   }
  1864. X
  1865. Xvoid Invert_Plane (Object)
  1866. X   OBJECT *Object;
  1867. X   {
  1868. X   PLANE *Plane = (PLANE  *) Object;
  1869. X
  1870. X   VScale (Plane->Normal_Vector, Plane->Normal_Vector, -1.0);
  1871. X   Plane->Distance *= -1.0;
  1872. X   }
  1873. X
  1874. END_OF_FILE
  1875. if test 6704 -ne `wc -c <'src/planes.c'`; then
  1876.     echo shar: \"'src/planes.c'\" unpacked with wrong size!
  1877. fi
  1878. # end of 'src/planes.c'
  1879. fi
  1880. if test -f 'src/spheres.c' -a "${1}" != "-c" ; then 
  1881.   echo shar: Will not clobber existing file \"'src/spheres.c'\"
  1882. else
  1883. echo shar: Extracting \"'src/spheres.c'\" \(7715 characters\)
  1884. sed "s/^X//" >'src/spheres.c' <<'END_OF_FILE'
  1885. X/*****************************************************************************
  1886. X*
  1887. X*                                      spheres.c
  1888. X*
  1889. X*   from DKBTrace (c) 1990  David Buck
  1890. X*
  1891. X*  This module implements the sphere primitive.
  1892. X*
  1893. X* This software is freely distributable. The source and/or object code may be
  1894. X* copied or uploaded to communications services so long as this notice remains
  1895. X* at the top of each file.  If any changes are made to the program, you must
  1896. X* clearly indicate in the documentation and in the programs startup message
  1897. X* who it was who made the changes. The documentation should also describe what
  1898. X* those changes were. This software may not be included in whole or in
  1899. X* part into any commercial package without the express written consent of the
  1900. X* author.  It may, however, be included in other public domain or freely
  1901. X* distributed software so long as the proper credit for the software is given.
  1902. X*
  1903. X* This software is provided as is without any guarantees or warranty. Although
  1904. X* the author has attempted to find and correct any bugs in the software, he
  1905. X* is not responsible for any damage caused by the use of the software.  The
  1906. X* author is under no obligation to provide service, corrections, or upgrades
  1907. X* to this package.
  1908. X*
  1909. X* Despite all the legal stuff above, if you do find bugs, I would like to hear
  1910. X* about them.  Also, if you have any comments or questions, you may contact me
  1911. X* at the following address:
  1912. X*
  1913. X*     David Buck
  1914. X*     22C Sonnet Cres.
  1915. X*     Nepean Ontario
  1916. X*     Canada, K2H 8W7
  1917. X*
  1918. X*  I can also be reached on the following bulleton boards:
  1919. X*
  1920. X*     ATX              (613) 526-4141
  1921. X*     OMX              (613) 731-3419
  1922. X*     Mystic           (613) 731-0088 or (613) 731-6698
  1923. X*
  1924. X*  Fidonet:   1:163/109.9
  1925. X*  Internet:  David_Buck@Carleton.CA
  1926. X*
  1927. X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  1928. X*
  1929. X*     Lattice BBS                      (708) 916-1200
  1930. X*     The Information Exchange BBS     (708) 945-5575
  1931. X*     Stillwaters BBS                  (708) 403-2826
  1932. X*
  1933. X*****************************************************************************/
  1934. X
  1935. X#include "frame.h"
  1936. X#include "vector.h"
  1937. X#include "dkbproto.h"
  1938. X
  1939. X#define Small_Tolerance 0.001
  1940. X
  1941. XMETHODS Sphere_Methods =
  1942. X   { Object_Intersect, All_Sphere_Intersections,
  1943. X     Inside_Sphere, Sphere_Normal,
  1944. X     Copy_Sphere,
  1945. X     Translate_Sphere, Rotate_Sphere,
  1946. X     Scale_Sphere, Invert_Sphere};
  1947. X
  1948. Xextern SPHERE *Get_Sphere_Shape();
  1949. X
  1950. Xextern RAY *VP_Ray;
  1951. Xextern long Ray_Sphere_Tests, Ray_Sphere_Tests_Succeeded;
  1952. X
  1953. Xint All_Sphere_Intersections (Object, Ray, Depth_Queue)
  1954. X  OBJECT *Object;
  1955. X  RAY *Ray;
  1956. X  PRIOQ *Depth_Queue;
  1957. X  {
  1958. X  DBL Depth1, Depth2;
  1959. X  VECTOR Intersection_Point;
  1960. X  INTERSECTION Local_Element;
  1961. X  register int Intersection_Found;
  1962. X  SPHERE *Shape = (SPHERE *) Object;
  1963. X
  1964. X  Intersection_Found = FALSE;
  1965. X  if (Intersect_Sphere (Ray, Shape, &Depth1, &Depth2))
  1966. X    {
  1967. X    Local_Element.Depth = Depth1;
  1968. X    Local_Element.Object = Shape -> Parent_Object;
  1969. X    VScale (Intersection_Point, Ray -> Direction, Depth1);
  1970. X    VAdd (Intersection_Point, Intersection_Point, Ray -> Initial);
  1971. X    Local_Element.Point = Intersection_Point;
  1972. X    Local_Element.Shape = (SHAPE *)Shape;
  1973. X    pq_add (Depth_Queue, &Local_Element);
  1974. X    Intersection_Found = TRUE;
  1975. X
  1976. X    if (Depth2 != Depth1)
  1977. X      {
  1978. X      Local_Element.Depth = Depth2;
  1979. X      Local_Element.Object = Shape -> Parent_Object;
  1980. X      VScale (Intersection_Point, Ray -> Direction, Depth2);
  1981. X      VAdd (Intersection_Point, Intersection_Point, Ray -> Initial);
  1982. X      Local_Element.Point = Intersection_Point;
  1983. X      Local_Element.Shape = (SHAPE *) Shape;
  1984. X      pq_add (Depth_Queue, &Local_Element);
  1985. X      Intersection_Found = TRUE;
  1986. X      }
  1987. X    }
  1988. X  return (Intersection_Found);
  1989. X  }
  1990. X
  1991. Xint Intersect_Sphere (Ray, Sphere, Depth1, Depth2)
  1992. X   RAY *Ray;
  1993. X   SPHERE *Sphere;
  1994. X   DBL *Depth1, *Depth2;
  1995. X   {
  1996. X   VECTOR Origin_To_Center;
  1997. X   DBL OCSquared, t_Closest_Approach, Half_Chord, t_Half_Chord_Squared;
  1998. X   short inside;
  1999. X
  2000. X   Ray_Sphere_Tests++;
  2001. X   if (Ray == VP_Ray) {
  2002. X      if (!Sphere->VPCached) {
  2003. X         VSub (Sphere->VPOtoC, Sphere->Center, Ray->Initial);
  2004. X         VDot (Sphere->VPOCSquared, Sphere->VPOtoC, Sphere->VPOtoC);
  2005. X         Sphere->VPinside = (Sphere->VPOCSquared < Sphere->Radius_Squared);
  2006. X         Sphere->VPCached = TRUE;
  2007. X         }
  2008. X      VDot (t_Closest_Approach, Sphere->VPOtoC, Ray->Direction);
  2009. X      if (!Sphere->VPinside && (t_Closest_Approach < Small_Tolerance))
  2010. X         return (FALSE);      
  2011. X      t_Half_Chord_Squared = Sphere->Radius_Squared - Sphere->VPOCSquared +
  2012. X                        (t_Closest_Approach * t_Closest_Approach);
  2013. X      }
  2014. X   else {
  2015. X      VSub (Origin_To_Center, Sphere->Center, Ray->Initial);
  2016. X      VDot (OCSquared, Origin_To_Center, Origin_To_Center);
  2017. X      inside = (OCSquared < Sphere->Radius_Squared);
  2018. X      VDot (t_Closest_Approach, Origin_To_Center, Ray->Direction);
  2019. X      if (!inside && (t_Closest_Approach < Small_Tolerance))
  2020. X         return (FALSE);
  2021. X
  2022. X      t_Half_Chord_Squared = Sphere->Radius_Squared - OCSquared +
  2023. X                        (t_Closest_Approach * t_Closest_Approach);
  2024. X      }
  2025. X
  2026. X   if (t_Half_Chord_Squared < Small_Tolerance)
  2027. X      return (FALSE);
  2028. X
  2029. X   Half_Chord = sqrt (t_Half_Chord_Squared);
  2030. X   *Depth1 = t_Closest_Approach + Half_Chord;
  2031. X   *Depth2 = t_Closest_Approach - Half_Chord;
  2032. X
  2033. X   if ((*Depth1 < Small_Tolerance) || (*Depth1 > Max_Distance))
  2034. X      if ((*Depth2 < Small_Tolerance) || (*Depth2 > Max_Distance))
  2035. X         return (FALSE);
  2036. X      else
  2037. X         *Depth1 = *Depth2;
  2038. X   else
  2039. X      if ((*Depth2 < Small_Tolerance) || (*Depth2 > Max_Distance))
  2040. X        *Depth2 = *Depth1;
  2041. X
  2042. X   Ray_Sphere_Tests_Succeeded++;
  2043. X   return (TRUE);
  2044. X   }
  2045. X
  2046. Xint Inside_Sphere (Point, Object)
  2047. X   VECTOR *Point;
  2048. X   OBJECT *Object;
  2049. X   {
  2050. X   VECTOR Origin_To_Center;
  2051. X   DBL OCSquared;
  2052. X   SPHERE *Sphere = (SPHERE *) Object;
  2053. X
  2054. X   VSub (Origin_To_Center, Sphere->Center, *Point);
  2055. X   VDot (OCSquared, Origin_To_Center, Origin_To_Center);
  2056. X
  2057. X   if (Sphere->Inverted)
  2058. X      return (OCSquared - Sphere->Radius_Squared > Small_Tolerance);
  2059. X   else
  2060. X      return (OCSquared - Sphere->Radius_Squared < Small_Tolerance);
  2061. X   }
  2062. X
  2063. Xvoid Sphere_Normal (Result, Object, Intersection_Point)
  2064. X   OBJECT *Object;
  2065. X   VECTOR *Result, *Intersection_Point;
  2066. X   {
  2067. X   SPHERE *Sphere = (SPHERE *) Object;
  2068. X
  2069. X   VSub (*Result, *Intersection_Point, Sphere->Center);
  2070. X   VScale (*Result, *Result, Sphere->Inverse_Radius);
  2071. X   Perturb_Normal (Result, Sphere -> Parent_Object,
  2072. X                   Intersection_Point, Result);
  2073. X  }
  2074. X
  2075. Xvoid *Copy_Sphere (Object)
  2076. X   OBJECT *Object;
  2077. X   {
  2078. X   SPHERE *New_Shape;
  2079. X
  2080. X   New_Shape = Get_Sphere_Shape ();
  2081. X   *New_Shape = *((SPHERE *) Object);
  2082. X   New_Shape -> Next_Object = NULL;
  2083. X   return (New_Shape);
  2084. X   }
  2085. X
  2086. Xvoid Translate_Sphere (Object, Vector)
  2087. X   OBJECT *Object;
  2088. X   VECTOR *Vector;
  2089. X   {
  2090. X   VAdd (((SPHERE *) Object)->Center, ((SPHERE *) Object)->Center, *Vector);
  2091. X   }
  2092. X
  2093. Xvoid Rotate_Sphere (Object, Vector)
  2094. X   OBJECT *Object;
  2095. X   VECTOR *Vector;
  2096. X   {
  2097. X   TRANSFORMATION Transformation;
  2098. X
  2099. X   Get_Rotation_Transformation (&Transformation, Vector);
  2100. X   MTransformVector (&((SPHERE *) Object)->Center,
  2101. X                     &((SPHERE *) Object)->Center, &Transformation);
  2102. X   }
  2103. X
  2104. Xvoid Scale_Sphere (Object, Vector)
  2105. X   OBJECT *Object;
  2106. X   VECTOR *Vector;
  2107. X   {
  2108. X   SPHERE *Sphere = (SPHERE *) Object;
  2109. X
  2110. X   if ((Vector->x != Vector->y) ||
  2111. X       (Vector->x != Vector->z)) {
  2112. X      printf ("Error - you cannot scale a sphere unevenly\n");
  2113. X      exit (1);
  2114. X      }
  2115. X
  2116. X   VScale (Sphere->Center, Sphere->Center, Vector->x);
  2117. X   Sphere->Radius *= Vector->x;
  2118. X   Sphere->Radius_Squared = Sphere->Radius * Sphere->Radius;
  2119. X   Sphere->Inverse_Radius = 1.0 / Sphere->Radius;
  2120. X   }
  2121. X
  2122. Xvoid Invert_Sphere (Object)
  2123. X   OBJECT *Object;
  2124. X   {
  2125. X   ((SPHERE *) Object)->Inverted ^= TRUE;
  2126. X   }
  2127. END_OF_FILE
  2128. if test 7715 -ne `wc -c <'src/spheres.c'`; then
  2129.     echo shar: \"'src/spheres.c'\" unpacked with wrong size!
  2130. fi
  2131. # end of 'src/spheres.c'
  2132. fi
  2133. echo shar: End of archive 3 \(of 10\).
  2134. cp /dev/null ark3isdone
  2135. MISSING=""
  2136. for I in 1 2 3 4 5 6 7 8 9 10 ; do
  2137.     if test ! -f ark${I}isdone ; then
  2138.     MISSING="${MISSING} ${I}"
  2139.     fi
  2140. done
  2141. if test "${MISSING}" = "" ; then
  2142.     echo You have unpacked all 10 archives.
  2143.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2144. else
  2145.     echo You still need to unpack the following archives:
  2146.     echo "        " ${MISSING}
  2147. fi
  2148. ##  End of shell archive.
  2149. exit 0
  2150. -- 
  2151. Mail submissions (sources or binaries) to <amiga@uunet.uu.net>.
  2152. Mail comments to the moderator at <amiga-request@uunet.uu.net>.
  2153. Post requests for sources, and general discussion to comp.sys.amiga.
  2154.